<?php

class Core_Model_DbTable_Likes extends Zend_Db_Table_Abstract {

    protected $_name = 'likes';

    public function getDetail($ids) {
        $buffer_likes_db = new Core_Model_DbTable_BufferLikes();
        $like_details = $buffer_likes_db->getDetail($ids)->toArray();
        $id_social_posts = array();
        foreach ($like_details as $like_detail) {
            $id_social_posts[] = $like_detail['id_social_post'];
        }
        $user_db = new Core_Model_DbTable_Users();
        $latest_access_token = $user_db->getLatestAccessToken();
        $params = "access_token=$latest_access_token";
        $facebook_wraper = new MKT_Facebook_Wraper();
        $posts = $facebook_wraper->getBatchData($id_social_posts, $params);
        foreach ($like_details as &$like_detail) {
            $id_social_post = $like_detail['id_social_post'];
            $post_data = $posts[$id_social_post];
            
            if (isset($post_data['message'])) {
                $like_detail['target_message'] = $post_data['message'];
            } elseif (isset($post_data['name'])) {
                $like_detail['target_message'] = $post_data['name'];
            }
            
            if (isset($post_data['picture'])) {
                $like_detail['picture'] = $post_data['picture'];
            }
            
            if (isset($post_data['from'])) {
                $like_detail['target_id_social_from'] = $post_data['from']['id'];
            } else {
                $like_detail['target_id_social_from'] = $like_detail['id_social_user'];
            }
            
            $like_detail['target_created_time'] = strtotime($post_data['updated_time']);
        }
        
        return $like_details;
    }
}
?>
